﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ART.Common;

namespace ART.Admini.Setting
{
    public partial class SchoolDicDetail : System.Web.UI.Page
    {
        protected int Id = 0;
        private ART.BLL.B_SchoolDic bll = new ART.BLL.B_SchoolDic();
        private ART.Model.M_SchoolDic model = new ART.Model.M_SchoolDic();
        private BLL.B_dictionary bll_dic = new BLL.B_dictionary();

        protected void Page_Load(object sender, EventArgs e)
        {
            Id = Common.LYRequest.GetInt("id", 0);
#warning BLL.B_TBAdmin.IsPermissions(0x6c)

            if (!IsPostBack)
            {
                InitData();
            }
        }
        private void InitData()
        {
            JBind.BindList(dropAreaId, bll_dic.GetList("parentid=1").Tables[0], "DictionaryName", "id");
            dropAreaId.Items.Insert(0, new ListItem("请选择", "0"));


            if (Id > 0)
            {//edit
                model = bll.GetModel(Id);
                if (model != null)
                {
                    txtSchoolName.Text = model.SchoolName;
                    if (model.AreaId != 0)
                    {
                        Model.M_dictionary modelDic = bll_dic.GetModel(model.AreaId);
                        if (modelDic.ParentId == 0) //一级
                        {
                            this.dropAreaId.SelectedValue = model.AreaId.ToString();
                        }
                        else if (bll_dic.GetModel(modelDic.ParentId).ParentId == 0) //二级
                        {
                            JBind.BindList(dropAreaId2, bll_dic.GetList("parentid=" + modelDic.ParentId).Tables[0], "DictionaryName", "id");
                            dropAreaId2.Items.Insert(0, new ListItem("请选择上级", "0"));
                            this.dropAreaId2.SelectedValue = modelDic.Id.ToString();
                            this.dropAreaId.SelectedValue = modelDic.ParentId.ToString();
                        }
                        else //三级
                        {
                            int parentid = bll_dic.GetModel(modelDic.ParentId).ParentId;
                            modelDic = bll_dic.GetModel(parentid);//sanji

                            JBind.BindList(dropAreaId2, bll_dic.GetList("parentid=" + parentid).Tables[0], "DictionaryName", "id");
                            dropAreaId2.Items.Insert(0, new ListItem("请选择上级", "0"));

                            JBind.BindList(dropAreaId3, bll_dic.GetList("parentid=" + modelDic.ParentId).Tables[0], "DictionaryName", "id");
                            dropAreaId3.Items.Insert(0, new ListItem("请选择上级", "0"));


                            this.dropAreaId2.SelectedValue = parentid.ToString();
                            this.dropAreaId.SelectedValue = bll_dic.GetModel(parentid).ParentId.ToString();
                            this.dropAreaId3.SelectedValue = model.Id.ToString();
                        }
                    }
                }
            }
        }
        protected void dropAreaId_SelectedIndexChanged(object sender, EventArgs p1)
        {
            if (this.dropAreaId.SelectedValue != "0")
            {
                dropAreaId2.Items.Clear();
                JBind.BindList(dropAreaId2, bll_dic.GetList("parentid=" + this.dropAreaId.SelectedValue).Tables[0], "DictionaryName", "id");
                dropAreaId2.Items.Insert(0, new ListItem("请选择市", "0"));
            }
        }

        protected void dropAreaId2_SelectedIndexChanged(object sender, EventArgs p1)
        {
            if (this.dropAreaId2.SelectedValue != "0")
            {
                dropAreaId3.Items.Clear();
                JBind.BindList(dropAreaId3, bll_dic.GetList("parentid=" + this.dropAreaId2.SelectedValue).Tables[0], "DictionaryName", "id");
                dropAreaId3.Items.Insert(0, new ListItem("请选择区", "0"));
            }
        }
        protected void btnSave_Click(object sender, EventArgs p1)
        {
            if (Id > 0)
            {
                #region __________Edit__________
                model = bll.GetModel(Id);
                model.SchoolName = txtSchoolName.Text;
                if (this.dropAreaId.SelectedValue != "0")
                {
                    if (!(this.dropAreaId2.SelectedValue != "0"))
                    {
                        model.AreaId = int.Parse(this.dropAreaId.SelectedValue);
                    }
                    else if (!(this.dropAreaId3.SelectedValue != "0"))
                    {
                        model.AreaId = int.Parse(this.dropAreaId2.SelectedValue);
                    }
                    else
                    {
                        model.AreaId = int.Parse(this.dropAreaId3.SelectedValue);
                    }
                }
                if (bll.Update(model))
                {
                    //B_operationLog.CreateOperationLog(B_operationLog.operationLogType.Edit, "", "");
                    MessageBox.JSLoad(sender, "alert('保存成功!');reloadDlg(true);");
                }
                #endregion
            }
            else
            {
                #region __________Add__________
                model.SchoolName = txtSchoolName.Text;
                if (this.dropAreaId.SelectedValue != "0")
                {
                    if (!(this.dropAreaId2.SelectedValue != "0"))
                    {
                        model.AreaId = int.Parse(this.dropAreaId.SelectedValue);
                    }
                    else if (!(this.dropAreaId3.SelectedValue != "0"))
                    {
                        model.AreaId = int.Parse(this.dropAreaId2.SelectedValue);
                    }
                    else
                    {
                        model.AreaId = int.Parse(this.dropAreaId3.SelectedValue);
                    }
                }
                bll.Add(model);
                //if (bll.Add(model) > 0)
                //{
                    //B_operationLog.CreateOperationLog(B_operationLog.operationLogType.Add, "", "");
                    MessageBox.JSLoad(sender, "alert('保存成功!');reloadDlg(true);");
                //}
                #endregion
            }
        }
    }
}
